/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.model;

import com.fandi.dao.CustomerDao;
import com.fandi.dao.impl.CustomerDaoImpl;
import com.fandi.entity.Customer;
import com.fandi.listener.CustomerListener;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fandiak
 */
public class CustomerModel extends Customer {

    private CustomerListener listener;

    public CustomerListener getListener() {
        return listener;
    }

    public void setListener(CustomerListener listener) {
        this.listener = listener;
    }
    
    @Override
    public void setId(int id) {
        super.setId(id); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setName(String name) {
        super.setName(name); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setCode(String code) {
        super.setCode(code); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setEmail(String email) {
        super.setEmail(email); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setPhone(String phone) {
        super.setPhone(phone); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setAddress(String address) {
        super.setAddress(address); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }

    @Override
    public void setDescription(String description) {
        super.setDescription(description); //To change body of generated methods, choose Tools | Templates.
        fireOnChange();
    }
    
    public void fireOnChange() {
        if (listener != null) {
            listener.onChange(this);
        }
    }
    
    public void insert() throws SQLException {
        CustomerModel model = new CustomerModel();
        model.setId(getId());
        model.setCode(getCode());
        model.setName(getName());
        model.setAddress(getAddress());
        model.setEmail(getEmail());
        model.setPhone(getPhone());
        model.setDescription(getDescription());
        
        CustomerDao dao = new CustomerDaoImpl();
        dao.insert(model);
    }
    
    public void update(String code) throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        CustomerModel model = dao.selectBy(code);
        
        model.setId(getId());
        model.setCode(getCode());
        model.setName(getName());
        model.setAddress(getAddress());
        model.setEmail(getEmail());
        model.setPhone(getPhone());
        model.setDescription(getDescription());
        model.setLastUpd(getLastUpd());
        
        dao.update(model);
    }
    
    public List<CustomerModel> selectAll() throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        List<CustomerModel> list = dao.selectAll();
        
        return list;
    }
    
    public CustomerModel selectById(String name) throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        CustomerModel model = dao.selectBy(name);
        
        return model;
    }
    
    public boolean delete(String code) throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        
        return dao.delete(code);
    }
    
    public List<CustomerModel> searchBy(String code) throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        List<CustomerModel> list = dao.searchBy(code);
        
        return list;
    }
    
    public int count() throws SQLException {
        CustomerDao dao = new CustomerDaoImpl();
        int count = dao.count();
        
        return count;
    }
    
    public void reset() {
        setCode("");
        setName("");
        setAddress("");
        setEmail("");
        setPhone("");
        setDescription("");
    }
}
